package com.tedu.drugs.service.impl;

import com.tedu.drugs.mapper.DrugsMapper;
import com.tedu.drugs.pojo.dto.DrugsParam;
import com.tedu.drugs.pojo.dto.DrugsQuery;
import com.tedu.drugs.pojo.entity.Medicines;
import com.tedu.drugs.pojo.vo.DrugsVO;
import com.tedu.drugs.service.DrugsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DrugsServiceImpl implements DrugsService {
    @Autowired
     DrugsMapper drugsMapper;
    @Override
    public List<DrugsVO> selectDrugs(DrugsQuery drugsQuery) {
        log.debug("查询药品信息的服务层：{}", drugsQuery);
        List<DrugsVO> list = drugsMapper.selectDrugs(drugsQuery);
        return list;
    }

    @Override
    public void insertDrugs(DrugsParam drugsParam) {
        log.debug("新增药品信息的服务层");
        System.out.println(drugsParam.getMedicineId());
        Medicines medicines = new Medicines();
        BeanUtils.copyProperties(drugsParam, medicines);
        if (medicines.getMedicineId()!=null) {
            //更新操作
            medicines.setUpdateTime(new Date());
            drugsMapper.updateDrugs(medicines);
        }else {
            //插入新值的操作（录入操作）
            medicines.setCreateTime(new Date());
            medicines.setUpdateTime(new Date());
            drugsMapper.insertDrugs(medicines);
        }
    }

    @Override
    public void delete(Long medicineId) {
        log.debug("删除:{}",medicineId);
        drugsMapper.delete(medicineId);
    }
}
